package com.equdao.car_violation_inquiry.controller;

import com.equdao.car_violation_inquiry.enums.ChannelStatusEnum;
import com.equdao.car_violation_inquiry.enums.ResultEnum;
import com.equdao.car_violation_inquiry.pojo.*;
import com.equdao.car_violation_inquiry.service.ChannelManageService;
import com.equdao.car_violation_inquiry.service.ClientService;
import com.equdao.car_violation_inquiry.utils.ResultUtil;
import com.equdao.car_violation_inquiry.utils.ResultViolationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * @Author: 杨忠强
 * @Date: 2018/5/28 16:10
 * @Description:
 */

@RestController

public class ClientController {


    @Autowired
    private ClientService clientService;

    @Autowired
    ChannelManageService channelManageService;

    /**
     * 查询历史违章记录
     */
    @PostMapping("/getViolationRecord")
    public ResponseUpstreamBean getViolationRecord(@Valid BaseViolationBean baseViolationBean, BindingResult bindingResult) {
        ResponseUpstreamBean responseUpstreamBean = new ResponseUpstreamBean();
        if (bindingResult.hasErrors()) {
            responseUpstreamBean = ResultViolationUtils.error(ResultEnum.MISMATCH.getCode(), bindingResult.getFieldError().getDefaultMessage());
            return responseUpstreamBean;
        }
        //查询请求用户信息
        ChannelManageDO channel = channelManageService.findChannelByChannelCode(baseViolationBean.getChannelCode());
        System.out.println(channel); //用户实体
        System.out.println(baseViolationBean);
        //判断用户令牌是否符合,开通状态
        if (channel == null){
            responseUpstreamBean = ResultViolationUtils.error(ResultEnum.ERROR.getCode(),"未知用户");
            return responseUpstreamBean;
        }
        if (null == baseViolationBean.getEngineNo() || null == baseViolationBean.getFrameNo()){
            responseUpstreamBean = ResultViolationUtils.error(ResultEnum.MISMATCH.getCode(),"车辆信息错误");
            return responseUpstreamBean;
        }




        if (baseViolationBean.getToken().equals(channel.getToken()) && channel.getStatus().equals(ChannelStatusEnum.OPEN.getCode())) {
            String cph =  baseViolationBean.getVehicleNo().substring(0,1);
            //判断用户次数是否够
            if (channel.getRemainNumber() > 1) {
                //保存客户请求信息
                // TODO 添加实体类判断
                baseViolationBean.setStatus("0");
                //判断用户请求哪个平台数据
                //请求万物数据
//                if (channel.getSupplier().equals("万物")) {
//                    baseViolationBean.setOrigin("万物");
//                    Integer integer = clientService.saveBaseCar(baseViolationBean);
//                    responseUpstreamBean = clientService.findViolationInfo(baseViolationBean, channel);
//                } else if (channel.getSupplier().equals("恒达")) {
//                    baseViolationBean.setOrigin("恒达");
//                    Integer integer = clientService.saveBaseCar(baseViolationBean);
//
//                    responseUpstreamBean = clientService.findWMViolationInfo(baseViolationBean, channel);
//                }
                if(cph.equals("浙") ){
                    baseViolationBean.setOrigin("万物");
                    Integer integer = clientService.saveBaseCar(baseViolationBean);
                    responseUpstreamBean = clientService.findViolationInfo(baseViolationBean,channel);
                }else if(!cph.equals("冀")){
                    baseViolationBean.setOrigin("恒达");
                    Integer integer = clientService.saveBaseCar(baseViolationBean);
                    responseUpstreamBean = clientService.findWMViolationInfo(baseViolationBean,channel);
                }
                //判断有没有数据   有数据则扣除用户剩余条数
            } else {
                responseUpstreamBean = ResultViolationUtils.error(ResultEnum.ORDER_DETAIL_EMPTY.getCode(), ResultEnum.ORDER_DETAIL_EMPTY.getMessage());
            }
        } else {
            responseUpstreamBean = ResultViolationUtils.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
        }
        return responseUpstreamBean;
    }


    /**
     * 查询全国违章记录
     */
    @PostMapping("/getViolationCurrent")
    public ResponseUpstreamBean getViolationCurrent(@Valid BaseViolationBean baseViolationBean, BindingResult bindingResult) {
        ResponseUpstreamBean responseUpstreamBean;
        if (bindingResult.hasErrors()) {
            responseUpstreamBean = ResultViolationUtils.error(ResultEnum.MISMATCH.getCode(), bindingResult.getFieldError().getDefaultMessage());
            return responseUpstreamBean;
        }
        //TODO 添加请求log日志
        //查询请求用户信息
        ChannelManageDO channel = channelManageService.findChannelByChannelCode(baseViolationBean.getChannelCode());
        if (channel == null){
            responseUpstreamBean = ResultViolationUtils.error(ResultEnum.ERROR.getCode(),"未知用户");
            return responseUpstreamBean;
        }

        System.out.println(channel); //用户实体
        System.out.println(baseViolationBean);
        //判断用户令牌是否符合,开通状态
        if (baseViolationBean.getToken().equals(channel.getToken()) && channel.getStatus().equals(ChannelStatusEnum.OPEN.getCode())) {
            //判断用户次数是否够
            if (channel.getRemainNumber() > 1) {
                //保存客户请求信息
                // TODO 添加实体类判断
                baseViolationBean.setStatus("0");
                //判断用户请求哪个平台数据
                //请求万物数据

                baseViolationBean.setOrigin("恒达");
                Integer integer = clientService.saveBaseCar(baseViolationBean);
                responseUpstreamBean = clientService.findHDQGViolationInfo(baseViolationBean, channel);

                //判断有没有数据   有数据则扣除用户剩余条数
            } else {
                responseUpstreamBean = ResultViolationUtils.error(ResultEnum.ORDER_DETAIL_EMPTY.getCode(), ResultEnum.ORDER_DETAIL_EMPTY.getMessage());
            }
        } else {
            responseUpstreamBean = ResultViolationUtils.error(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMessage());
        }
        return responseUpstreamBean;
    }


}
